#ifndef _NE_MIDI_FILE_FORMAT_H_
#define _NE_MIDI_FILE_FORMAT_H_

#include <neinit.h>

namespace ne
{
    
    const char MIDI_HEADER_ID[4] = {'M', 'T', 'h', 'd'}; // 0x4D546864
    const char MIDI_TRACK_ID[4]  = {'M', 'T', 'r', 'k'}; // 0x4D54726B

    const byte MIDI_EVENT_MASK   = 0xF0;
    const byte MIDI_CHANNEL_MASK = 0x0F;
    const byte MIDI_DATA_MASK    = 0x7F;
    const byte MIDI_DATA_FLAG    = 0x80;

    const uint16_t MIDI_TIME_DIVISION_MASK = 0x7FFF;
    const uint16_t MIDI_TIME_DIVISION_FLAG = 0x8000;

    // BPM: beats per minute
    // BPM = MICROSECONDS_PER_MINUTE / MPQN

    // MPQN: microseconds per quarter-note
    // MPQN = MICROSECONDS_PER_MINUTE / BPM

    // MPT: microseconds per tick
    // TPQN: ticks per quarter-note

    const uint32_t MIDI_MICROSECONDS_PER_SECOND = 1000000;
    const uint32_t MIDI_MICROSECONDS_PER_MINUTE = 60000000;
    const uint32_t MIDI_DEFAULT_BPM = 120;
    const uint32_t MIDI_DEFAULT_MPQN = 500000;
    const uint32_t MIDI_DEFAULT_TPQN = 480;

    struct MIDIHeaderChunk
    {
        char mID[4];
        uint32_t mSize;
        uint16_t mFormat;
        uint16_t mNumTracks;
        uint16_t mTimeDivision;
    };

    struct MIDITrackChunk
    {
        char mID[4];
        uint32_t mSize;
    };
}

#endif
